﻿using UnityEngine;

namespace 凌依.工具库.U3D渲染
{
    /// <summary>
    /// 可修改内部参数
    /// </summary>
    [System.Serializable]
    public class 绘制圆
    {
        private LineRenderer 线;
        public float 半径;
        public int 点数;
        public Color 色彩;
        public float 宽度;
        public bool _2D;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="材质"></param>
        /// <param name="主体"></param>
        /// <param name="色彩"></param>
        /// <param name="宽度"></param>
        /// <param name="半径"></param>
        /// <param name="点数"></param>
        /// <param name="_2D"></param>
        public 绘制圆(Material 材质, Transform 主体, Color 色彩, float 宽度, float 半径, int 点数, bool _2D)
        {
            线 = new GameObject("范围").AddComponent<LineRenderer>();
            线.transform.SetParent(主体);
            线.transform.localPosition = Vector3.zero;
            线.transform.localRotation = Quaternion.identity;
            线.startWidth = 宽度;
            this.宽度 = 宽度;
            this.色彩 = 色彩;
            线.startColor = 色彩;
            线.endColor = 色彩;
            线.material = 材质;
            this.半径 = 半径;
            线.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
            线.receiveShadows = false;
            this.点数 = 点数;
            this._2D = _2D;
            线.useWorldSpace = false;
        }
        /// <summary>
        /// 每帧执行,用于绘制
        /// </summary>
        public void 刷新()
        {
            if (线 == null)
            {
                Debug.LogError("请先初始化 绘制圆");
                return;
            }
            if (_2D)
            {
                线.positionCount = 点数 + 1;
                线.startWidth = 宽度;
                for (int i = 0; i < 点数 + 1; i++)
                {
                    float x = 半径 * Mathf.Cos((360f / 点数 * i) * Mathf.Deg2Rad); //确定x坐标 
                    float y = 半径 * Mathf.Sin((360f / 点数 * i) * Mathf.Deg2Rad); //确定z坐标
                    线.SetPosition(i, new Vector3(x, y, 线.transform.position.z));
                }
            }
            else
            {
                线.positionCount = 点数 + 1;
                线.startWidth = 宽度;
                for (int i = 0; i < 点数 + 1; i++)
                {
                    float x = 半径 * Mathf.Cos((360f / 点数 * i) * Mathf.Deg2Rad);
                    float z = 半径 * Mathf.Sin((360f / 点数 * i) * Mathf.Deg2Rad);
                    线.SetPosition(i, new Vector3(x, 线.transform.position.y, z));
                }
            }
        }
    }

    public class 绘制线
    {
        public LineRenderer 线;
        public Color 色彩;
        public float 宽度;

        public 绘制线(Material 材质, Transform 主体, Color 色彩, float 宽度)
        {
            线 = new GameObject("射线").AddComponent<LineRenderer>();
            线.transform.SetParent(主体);
            线.transform.localPosition = Vector3.zero;
            线.transform.localRotation = Quaternion.identity;
            线.startWidth = 宽度;
            this.宽度 = 宽度;
            this.色彩 = 色彩;
            线.startColor = 色彩;
            线.endColor = 色彩;
            线.material = 材质;
            线.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
            线.receiveShadows = false;
            线.useWorldSpace = false;
            线.positionCount = 2;
        }

        public void 设置线(Vector3 开始,Vector3 结束)
        {
            线.SetPosition(0,开始);
            线.SetPosition(1,结束);
        }

    }
}
